package com.zzxx.service.impl;

import com.zzxx.entity.*;
import com.zzxx.mapper.ActivityContentMapper;
import com.zzxx.mapper.GymContentMapper;
import com.zzxx.mapper.UserContentMapper;
import com.zzxx.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private GymContentMapper gymContentMapper;
    @Autowired
    private ActivityContentMapper activityContentMapper;
    @Autowired
    private UserContentMapper userContentMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public List<GymContent> list(String id) {
        GymContentExample gymContentExample = new GymContentExample();
        GymContentExample.Criteria criteria = gymContentExample.createCriteria().andGymidEqualTo(id);
        List<GymContent> list = gymContentMapper.selectByExample1(gymContentExample);
        for(GymContent c:list){
                c.setIslike(true);
        }
        return list;
    }
    @Override
    public void addlike(String cid, String uid) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        UserContent userContent = new UserContent();
        userContent.setId(uuid);
        userContent.setContentid(cid);
        userContent.setUserid(uid);
        userContentMapper.insertSelective(userContent);
        GymContent gymContent = gymContentMapper.selectByPrimaryKey(cid);
        gymContent.setLikenum(gymContent.getLikenum()+1);
        gymContentMapper.updateByPrimaryKeySelective(gymContent);
    }

    @Override
    public List<UserContent> findlike(String uid) {
        UserContentExample userContentExample = new UserContentExample();
        UserContentExample.Criteria criteria = userContentExample.createCriteria().andUseridEqualTo(uid);
        List<UserContent> list = userContentMapper.selectByExample(userContentExample);
        return list;
    }

    @Override
    public void deletelike(String cid, String uid) {
        UserContentExample userContentExample = new UserContentExample();
        UserContentExample.Criteria criteria = userContentExample.createCriteria().andContentidEqualTo(cid).andUseridEqualTo(uid);
        userContentMapper.deleteByExample(userContentExample);
        GymContent gymContent = gymContentMapper.selectByPrimaryKey(cid);
        gymContent.setLikenum(gymContent.getLikenum()-1);
        gymContentMapper.updateByPrimaryKeySelective(gymContent);
    }

    @Override
    public void send(GymContent gymContent) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        gymContent.setId(uuid);
        gymContent.setLikenum(0);
        gymContent.setCreatetime(new Date());
        gymContentMapper.insertSelective(gymContent);
    }

    @Override
    public void deleteContent(String cid) {
        gymContentMapper.deleteByPrimaryKey(cid);
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        String uid = loginUser.getId();
        UserContentExample userContentExample = new UserContentExample();
        UserContentExample.Criteria criteria = userContentExample.createCriteria().andContentidEqualTo(cid).andUseridEqualTo(uid);
        userContentMapper.deleteByExample(userContentExample);
    }

    @Override
    public GymContent findOne(String cid) {
        GymContent gymContent = gymContentMapper.selectByPrimaryKey(cid);
        return gymContent;
    }

    @Override
    public void updateContent(GymContent gymContent) {
        gymContentMapper.updateByPrimaryKeySelective(gymContent);
    }

    @Override
    public void deleteACContent(String cid) {
        activityContentMapper.deleteByPrimaryKey(cid);
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        String uid = loginUser.getId();
        UserContentExample userContentExample = new UserContentExample();
        userContentExample.createCriteria().andContentidEqualTo(cid).andUseridEqualTo(uid);
        userContentMapper.deleteByExample(userContentExample);
    }

    @Override
    public void updateACContent(ActivityContent activityContent) {
        activityContentMapper.updateByPrimaryKeySelective(activityContent);
    }

    @Override
    public ActivityContent findACOne(String cid) {
        return activityContentMapper.selectByPrimaryKey(cid);
    }

    @Override
    public List<ActivityContent> list2(String id) {
        ActivityContentExample example = new ActivityContentExample();
        example.createCriteria().andActivityidEqualTo(id);
        List<ActivityContent> list = activityContentMapper.selectByExample1(example);
        for(ActivityContent c:list){
            c.setIslike(false);
        }
        return list;
    }

    @Override
    public void sendA(ActivityContent activityContent) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        activityContent.setLikenum(0);
        activityContent.setId(uuid);
        activityContent.setCreatetime(new Date());
        activityContentMapper.insertSelective(activityContent);
    }

    @Override
    public void addAClike(String cid, String uid) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        UserContent userContent = new UserContent();
        userContent.setId(uuid);
        userContent.setContentid(cid);
        userContent.setUserid(uid);
        userContentMapper.insertSelective(userContent);
        ActivityContent activityContent = activityContentMapper.selectByPrimaryKey(cid);
        activityContent.setLikenum(activityContent.getLikenum()+1);
        activityContentMapper.updateByPrimaryKeySelective(activityContent);
    }

    @Override
    public void deleteAClike(String cid, String uid) {
        UserContentExample userContentExample = new UserContentExample();
        userContentExample.createCriteria().andContentidEqualTo(cid).andUseridEqualTo(uid);
        userContentMapper.deleteByExample(userContentExample);
        ActivityContent activityContent = activityContentMapper.selectByPrimaryKey(cid);
        activityContent.setLikenum(activityContent.getLikenum()-1);
        activityContentMapper.updateByPrimaryKeySelective(activityContent);
    }

}
